/*   1:    */ package org.jetbrains.rpc;
/*   2:    */ 
/*   3:    */

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.containers.ConcurrentIntObjectMap;
import com.intellij.util.containers.StripedLockIntObjectConcurrentHashMap;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Arrays;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */ 
/*  10:    */ public final class MessageManager<OUTGOING, INCOMING, INCOMING_WITH_SEQ, SUCCESS, ERROR_DETAILS>
/*  11:    */ {
/*  12: 17 */   public static final Logger LOG = Logger.getInstance(MessageManager.class);
/*  13: 19 */   private final ConcurrentIntObjectMap<AsyncResultCallback<SUCCESS, ERROR_DETAILS>> a = new StripedLockIntObjectConcurrentHashMap();
/*  14:    */   private final Handler<OUTGOING, INCOMING, INCOMING_WITH_SEQ, SUCCESS, ERROR_DETAILS> b;
/*  15:    */   private volatile boolean c;
/*  16:    */   
/*  17:    */   public MessageManager(Handler<OUTGOING, INCOMING, INCOMING_WITH_SEQ, SUCCESS, ERROR_DETAILS> paramHandler)
/*  18:    */   {
/*  19: 25 */     this.b = paramHandler;
/*  20:    */   }
/*  21:    */   
/*  22:    */   public void send(@NotNull OUTGOING paramOUTGOING, @NotNull AsyncResultCallback<SUCCESS, ERROR_DETAILS> paramAsyncResultCallback)
/*  23:    */   {
/*  24: 43 */     if (paramOUTGOING == null) {
/*  25: 43 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/rpc/MessageManager", "send" }));
/*  26:    */     }
/*  27: 43 */     if (paramAsyncResultCallback == null) {
/*  28: 43 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "1", "org/jetbrains/rpc/MessageManager", "send" }));
/*  29:    */     }
/*  30: 43 */     if (this.c)
/*  31:    */     {
/*  32: 44 */       paramAsyncResultCallback.onError("Connection is closed", null);
/*  33: 45 */       return;
/*  34:    */     }
/*  35: 48 */     int i = this.b.getUpdatedSequence(paramOUTGOING);
/*  36: 49 */     this.a.put(i, paramAsyncResultCallback);
/*  37: 50 */     a(paramOUTGOING, i);
/*  38:    */   }
/*  39:    */   
/*  40:    */   private void a(@NotNull OUTGOING paramOUTGOING, int paramInt)
/*  41:    */   {
/*  42: 56 */     if (paramOUTGOING == null) {
/*  43: 56 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/rpc/MessageManager", "doSend" }));
/*  44:    */     }
/*  45:    */     boolean bool;
/*  46:    */     try
/*  47:    */     {
/*  48: 56 */       bool = this.b.write(paramOUTGOING);
/*  49:    */     }
/*  50:    */     catch (Throwable localThrowable)
/*  51:    */     {
/*  52:    */       try
/*  53:    */       {
/*  54: 60 */         a(paramInt);
/*  55:    */       }
/*  56:    */       finally
/*  57:    */       {
/*  58: 63 */         LOG.error("Failed to send", localThrowable);
/*  59:    */       }
/*  60: 65 */       return;
/*  61:    */     }
/*  62: 68 */     if (!bool) {
/*  63: 69 */       a(paramInt);
/*  64:    */     }
/*  65:    */   }
/*  66:    */   
/*  67:    */   private void a(int paramInt)
/*  68:    */   {
/*  69: 74 */     AsyncResultCallback localAsyncResultCallback = (AsyncResultCallback)this.a.remove(paramInt);
/*  70: 75 */     if (localAsyncResultCallback != null) {
/*  71: 76 */       localAsyncResultCallback.onError("Failed to send", null);
/*  72:    */     }
/*  73:    */   }
/*  74:    */   
/*  75:    */   public void processIncoming(INCOMING paramINCOMING)
/*  76:    */   {
/*  77: 81 */     Object localObject = this.b.readIfHasSequence(paramINCOMING);
/*  78: 82 */     if (localObject == null)
/*  79:    */     {
/*  80: 83 */       if (this.c) {
/*  81: 85 */         LOG.info("Connection closed, ignore incoming");
/*  82:    */       } else {
/*  83: 88 */         this.b.acceptNonSequence(paramINCOMING);
/*  84:    */       }
/*  85: 90 */       return;
/*  86:    */     }
/*  87: 93 */     AsyncResultCallback localAsyncResultCallback = getCallbackAndRemove(this.b.getSequence(localObject));
/*  88:    */     try
/*  89:    */     {
/*  90: 95 */       if (this.c) {
/*  91: 96 */         localAsyncResultCallback.onError("Connection closed", null);
/*  92:    */       } else {
/*  93: 99 */         this.b.call(localObject, localAsyncResultCallback);
/*  94:    */       }
/*  95:    */     }
/*  96:    */     catch (Throwable localThrowable)
/*  97:    */     {
/*  98:103 */       localAsyncResultCallback.onError("Failed to dispatch response to callback", null);
/*  99:104 */       LOG.error("Failed to dispatch response to callback", localThrowable);
/* 100:    */     }
/* 101:    */   }
/* 102:    */   
/* 103:    */   public AsyncResultCallback<SUCCESS, ERROR_DETAILS> getCallbackAndRemove(int paramInt)
/* 104:    */   {
/* 105:109 */     AsyncResultCallback localAsyncResultCallback = (AsyncResultCallback)this.a.remove(paramInt);
/* 106:110 */     if (localAsyncResultCallback == null) {
/* 107:111 */       throw new IllegalArgumentException("Cannot find callback with id " + paramInt);
/* 108:    */     }
/* 109:113 */     return localAsyncResultCallback;
/* 110:    */   }
/* 111:    */   
/* 112:    */   public void closed()
/* 113:    */   {
/* 114:117 */     this.c = true;
/* 115:    */   }
/* 116:    */   
/* 117:    */   public void cancelWaitingRequests()
/* 118:    */   {
/* 119:122 */     ConcurrentIntObjectMap localConcurrentIntObjectMap = this.a;
/* 120:123 */     int[] arrayOfInt1 = localConcurrentIntObjectMap.keys();
/* 121:124 */     Arrays.sort(arrayOfInt1);
/* 122:125 */     for (int k : arrayOfInt1) {
/* 123:    */       try
/* 124:    */       {
/* 125:127 */         ((AsyncResultCallback)localConcurrentIntObjectMap.get(k)).onError("Connection closed", null);
/* 126:    */       }
/* 127:    */       catch (Throwable localThrowable)
/* 128:    */       {
/* 129:130 */         LOG.error("Failed to reject callback on connection closed", localThrowable);
/* 130:    */       }
/* 131:    */     }
/* 132:    */   }
/* 133:    */   
/* 134:    */   public static abstract interface Handler<OUTGOING, INCOMING, INCOMING_WITH_SEQ, SUCCESS, ERROR_DETAILS>
/* 135:    */   {
/* 136:    */     public abstract int getUpdatedSequence(OUTGOING paramOUTGOING);
/* 137:    */     
/* 138:    */     public abstract boolean write(@NotNull OUTGOING paramOUTGOING)
/* 139:    */       throws IOException;
/* 140:    */     
/* 141:    */     public abstract INCOMING_WITH_SEQ readIfHasSequence(INCOMING paramINCOMING);
/* 142:    */     
/* 143:    */     public abstract int getSequence(INCOMING_WITH_SEQ paramINCOMING_WITH_SEQ);
/* 144:    */     
/* 145:    */     public abstract void acceptNonSequence(INCOMING paramINCOMING);
/* 146:    */     
/* 147:    */     public abstract void call(INCOMING_WITH_SEQ paramINCOMING_WITH_SEQ, AsyncResultCallback<SUCCESS, ERROR_DETAILS> paramAsyncResultCallback);
/* 148:    */   }
/* 149:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.rpc.MessageManager

 * JD-Core Version:    0.7.0.1

 */